package com.systemframework.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author zhanminghua
 * @date 2020-12-01 13:28
 */
public class ExcelExport {

    /**
     * 导出指定的excel
     * @param cols 指定的列
     * @param lists 数据
     * @param response 返回
     * @param sheetName 列名
     * @param fileName 文件名
     * @param <T>
     * @throws IOException
     */
    public  static <T>  void exportExcel(List<Map<String,Object>> cols , List<T> lists, HttpServletResponse response, String sheetName, String fileName,String fieldName,String fieldLabel) throws IOException {
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);

        ExportParams params = new ExportParams();
        params.setSheetName(sheetName);
        params.setType(ExcelType.HSSF);
        List<ExcelExportEntity> colList = new ArrayList<>();
        cols.forEach(col->{
            ExcelExportEntity excelExportEntity=new ExcelExportEntity(StrUtil.toString(col.get(fieldName)),col.get(fieldLabel));
            colList.add(excelExportEntity);
        });

        Workbook workbook = ExcelExportUtil.exportExcel(params, colList,lists);
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }


    /**
     * 不指定列导出
     * @param lists 数据
     * @param t 类
     * @param response 返回
     * @param sheetName 列名
     * @param fileName 文件名
     * @throws IOException
     */
    public static <T> void exportExcel(List<T> lists,Class<T> t, HttpServletResponse response,String sheetName,String fileName) throws IOException {
        // 设置响应输出的头类型
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);
        ExportParams deptExportParams = new ExportParams();
        // 设置sheet得名称
        deptExportParams.setSheetName(sheetName);
        Map<String, Object> deptExportMap = MapUtil.newHashMap(4);
        // title的参数为ExportParams类型，目前仅仅在ExportParams中设置了sheetName
        deptExportMap.put("title", deptExportParams);
        // 模版导出对应得实体类型
        deptExportMap.put("entity", t);
        // sheet中要填充得数据
        deptExportMap.put("data", lists);

        List<Map<String, Object>> sheetsList = new ArrayList<>();
        sheetsList.add(deptExportMap);
        Workbook workbook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();

    }





}
